"""
平滑处理模块
"""

import numpy as np
from scipy.signal import savgol_filter
from .config import CONFIG


class SmoothingProcessor:
    """平滑处理器类"""
    
    def sg_smooth(self, data: np.ndarray, 
                  window_length: int = None, 
                  polyorder: int = None) -> np.ndarray:
        """
        Savitzky-Golay平滑处理
        
        Args:
            data: 输入光谱数据
            window_length: 窗口长度
            polyorder: 多项式阶数
            
        Returns:
            np.ndarray: 平滑后的数据
        """
        if window_length is None:
            window_length = CONFIG['sg_window_length']
        if polyorder is None:
            polyorder = CONFIG['sg_polyorder']
        
        # 确保窗口长度为奇数且小于数据长度
        if window_length % 2 == 0:
            window_length += 1
        if window_length > data.shape[1]:
            window_length = data.shape[1] if data.shape[1] % 2 == 1 else data.shape[1] - 1
        
        # 对每一行（每个样本）应用SG平滑
        smoothed_data = np.zeros_like(data)
        for i in range(data.shape[0]):
            smoothed_data[i] = savgol_filter(data[i], window_length, polyorder)
        
        return smoothed_data
    
    def no_smooth(self, data: np.ndarray) -> np.ndarray:
        """
        无平滑处理（返回原始数据）
        
        Args:
            data: 输入光谱数据
            
        Returns:
            np.ndarray: 原始数据
        """
        return data.copy()